<?php if (!defined('THINK_PATH')) exit();?><link rel="stylesheet" type="text/css" href="/StudentRegistration/Public/Home/css/seat.css" />
<style>
    .disDorm_table{
        border-collapse:separate;
        border-spacing:0px 12px;
        margin:12px auto;
    }
    .disDorm_table td{
        padding:0px 10px 0px 10px;
    }
    .dialog-button {
        text-align: center;
    }
</style>
<div class="easyui-layout" data-options="fit:true">
    <div data-options="region:'center',border:false">
        <!-- Begin of toolbar -->
        <div id="student-toolbar">
            <div class="wu-toolbar-button">
                <a class="easyui-linkbutton" href="#" iconcls="icon-reload" onclick="searchStudent()" plain="true">
                    刷新
                </a>
                <a class="easyui-linkbutton" href="#" id="exportData" iconcls="icon-print" onclick="exportData()" plain="true">
                    导出
                </a>
                <a class="easyui-linkbutton" href="#" iconcls="icon-back" onclick="removeTab()" plain="true">
                    返回
                </a>
            </div>
            <div class="wu-toolbar-search">
                <label>
                    院系：
                </label>
                <select class="easyui-combobox" editable="false" id="q_department" name="q_department" panelheight="auto" style="width:160px;">
                    <option value="-1">请选择</option>
                </select>
                <label>
                    专业：
                </label>
                <select class="easyui-combobox" editable="false" id="q_major" name="q_major" style="width:160px;">
                    <option value="-1">请选择</option>
                </select>
                <label>
                    姓名：
                </label>
                <input class="easyui-textbox" data-options="prompt:'姓名'" id="q_studentName" name="q_studentName" style="width:120px;" type="text"/>
                <label>
                    寝室：
                </label>
                <select class="easyui-combobox" editable="false" id="q_dorm" name="q_dorm" style="width:160px;">
                    <option value="-1">请选择</option>
                </select>
                <a class="easyui-linkbutton" id="regisQueryBtn" href="#" iconcls="icon-search" onclick="searchStudent()" style="margin-left:30px;">
                    开始检索
                </a>
            </div>
        </div>
        <!-- End of toolbar -->
        <table class="easyui-datagrid" id="student-datagrid" toolbar="#student-toolbar">
        </table>
    </div>
</div>
<!-- Begin of easyui-dialog -->
<div id="disDorm-dialog" class="easyui-dialog" data-options="closed:true,iconCls:'icon-save'"
     style="width:600px; padding:10px;">
    <form id="disDorm-form" method="post">
        <table class="disDorm_table">
            <tr>
                <td width="20%" align="right">姓名:</td>
                <td>
                    <input type="text" name="disDorm_studentname" class="easyui-textbox" readonly="readonly" id="disDorm_studentname" style="width:120px" />
                </td>
                <td width="20%" align="right">性别:</td>
                <td>
                    <input type="text" name="disDorm_studentsex" class="easyui-textbox" readonly="readonly" id="disDorm_studentsex" style="width:80px" />
                </td>
            </tr>
            <tr>
                <td width="20%" align="right">院系:</td>
                <td>
                    <input type="text" name="disDorm_department" class="easyui-textbox" readonly="readonly" id="disDorm_department" style="width:120px" />
                </td>
                <td width="20%" align="right">专业:</td>
                <td>
                    <input type="text" name="disDorm_major" class="easyui-textbox" readonly="readonly" id="disDorm_major" style="width:180px" />
                </td>
            </tr>
            <tr>
                <td align="right">寝室:</td>
                <td colspan="3">
                    <select class="easyui-combobox" editable="false" id="dis_dorm" name="dis_dorm" style="width:160px;">
                        <option value="-1">请选择</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td colspan="4" align="center">
                    <div class="container">
                        <div class="demo clearfix">
                            <div class="front">寝室结构图</div>
                            <div id="seat_area">
                            </div>
                            <div id="legend"></div>
                        </div>
                    </div>
                </td>
            </tr>
        </table>
    </form>
</div>
<!-- End of easyui-dialog -->
<script>
    var last_seat = '';
    var un_select = '';
    var sc;
    var studentname = new Array();
    var map = [];
    var unavailable = [];
    var select = [];
    var dormSpec = 0;
    var old_dormcode = -1;
    var dormposition = '';
    var p_dormcode = -1;
    var sid = '';
    $(function() {
        $('#student-datagrid').datagrid({
            url:studentListUrl,
            rownumbers: true,
            pagination: true,
            fitColumns: true,
            fit: true,
            singleSelect: true,
            pageSize: 10,
            pageList: [10, 50, 100],
            remoteSort: true,
            multiSort: true,
            columns: [
                [{
                    field: 'id',
                    title: 'ID',
                    align: 'center',
                    hidden:'true',
                    width: 40
                }, {
                    field: 'studentname',
                    title: '姓名',
                    width: 80,
                    align: 'center'
                }, {
                    field: 'sexcode',
                    title: '性别',
                    width: 20,
                    hidden:'true',
                    align: 'center'
                },{
                    field: 'sexvalue',
                    title: '性别',
                    width: 40,
                    align: 'center'
                },{
                    field: 'birthday',
                    title: '出生日期',
                    width: 150,
                    align: 'center',
                    formatter: format_date
                }, {
                    field: 'nationvalue',
                    title: '民族',
                    width: 80,
                    align: 'center'
                },{
                    field: 'departmentcode',
                    title: '院系',
                    width: 20,
                    hidden:'true',
                    align: 'center'
                }, {
                    field: 'departmentvalue',
                    title: '院系',
                    width: 140,
                    align: 'center'
                },{
                    field: 'majorcode',
                    title: '专业',
                    width: 20,
                    hidden:'true',
                    align: 'center'
                }, {
                    field: 'majorvalue',
                    title: '专业',
                    width: 170,
                    align: 'center'
                }, {
                    field: 'dormcode',
                    title: '寝室',
                    width: 20,
                    hidden:'true',
                    align: 'center'
                }, {
                    field: 'dormvalue',
                    title: '寝室',
                    width: 140,
                    align: 'center'
                },{
                    field: 'dormposition',
                    title: '寝室床位号',
                    width: 140,
                    hidden:'true',
                    align: 'center'
                },{
                    field: 'registrationdate',
                    title: '报到日期',
                    width: 180,
                    align: 'center',
                    formatter: format_date
                }, {
                    field: 'opt',
                    title: '操作',
                    width: 120,
                    align: 'center',
                    formatter: showStudentBtn
                }]
            ],
            queryParams: {
                q_studentName: "",
                q_department: -1,
                q_major: -1,
                q_dorm: -1
            },
            onLoadSuccess: function(data) {
                $("a[name='disDorm']").linkbutton({
                    plain: true,
                    iconCls: 'icon-tip'
                });
            }
        });
        getDepartmentData('#q_department');
        getDormData('#q_dorm',-1,-1);
    });
    //加载院系数据
    function getDepartmentData(selectorId){
        $.ajax({
            url: departmentUrl,
            datatype: 'json',
            type: 'post',
            success: function(result) {
                for (i=0;i<result.length;i++) {
                    $(selectorId).append('<option value=' + result[i].id + '>' + result[i].department + '</option>');
                }
            $(selectorId).combobox({
                onChange:function(new_value,old_value){
                        department_onChange(new_value);
                }
            });
            $(selectorId).combobox({});
            },
            async: false
        });
    }
    //加载寝室数据
    function getDormData(selectorId,deptId,typeId){
        $.ajax({
            url: dormUrl,
            datatype: 'json',
            type: 'post',
            data:'deptId='+deptId + '&typeId=' + typeId,
            success: function(result) {
                for (i=0;i<result.length;i++) {
                    $(selectorId).append('<option value=' + result[i].id + '>' + result[i].dorm + '</option>');
                }
                $(selectorId).combobox({});
            },
            async: false
        });
    }
    function department_onChange(deptId) {
        $('#q_major').empty();
        $('#q_major').append('<option value=-1>请选择</option>');
        $('#q_dorm').empty();
        $('#q_dorm').append('<option value=-1>请选择</option>');
        getMajorData('#q_major', deptId);
        getDormData('#q_dorm',deptId,-1);
    }
    //格式化日期列
    function format_date(value, row, index) {
        return toDateString(value * 1000);
    }
    //显示操作按钮
    function showStudentBtn(value, row, index) {
        return "<a href='#' onclick='disDorm(" + index + ")' id='disDorm' class='easyui-linkbutton' name='disDorm'>分配寝室</a>";
    }
    //按条件查询表格数据
    function searchStudent() {
        var queryParams = $('#student-datagrid').datagrid('options').queryParams;
        queryParams.q_studentName = $.trim($('input[textboxname=q_studentName]').textbox('getValue'));
        queryParams.q_department = $("#q_department").combobox('getValue');
        queryParams.q_major = $("#q_major").combobox('getValue');
        queryParams.q_dorm = $("#q_dorm").combobox('getValue');
        $('#student-datagrid').datagrid('reload');
    }

    //分配寝室
    function disDorm(rowIndex) {
        //获取当前选中行的索引
        selectedRowIndex = $('#student-datagrid').datagrid('getRowIndex', $('#student-datagrid').datagrid('getSelected'));
        //判断当前按钮所在行与当前选中行是否一致
        if (rowIndex == selectedRowIndex) {
            row = $('#student-datagrid').datagrid('getSelected');
            if (row) {
                //初始化表单中的数据
                $('input[textboxname=disDorm_studentname]').textbox('setValue', row['studentname']);
                $('input[textboxname=disDorm_studentsex]').textbox('setValue', row['sexvalue']);
                $('input[textboxname=disDorm_department]').textbox('setValue', row['departmentvalue']);
                $('input[textboxname=disDorm_major]').textbox('setValue', row['majorvalue']);
                dormposition = row['dormposition'];
                sid = row['id'];
                $("#dis_dorm").empty();
                $('#dis_dorm').append('<option value=-1>请选择</option>');
                $.ajax({
                    url: dormUrl,
                    datatype: 'json',
                    type: 'post',
                    data:'deptId=' + row['departmentcode'] + '&typeId=' + row['sexcode'],
                    success: function(result) {
                        for (i=0;i<result.length;i++) {
                            $('#dis_dorm').append('<option value=' + result[i].id + '>' + result[i].dorm + '</option>');
                        }
                        $("#dis_dorm").combobox({
                            onChange:function(new_value,old_value){
                                dormChange(new_value);
                            }
                        });
                        $('#dis_dorm').combobox({});
                    },
                    async: false
                });
                last_seat = '';
                un_select = '';
                studentname = new Array();
                map = [];
                unavailable = [];
                select = [];
                dormSpec = 0;
                old_dormcode = row['dormcode'];
                //判断当前学生有没有分配过寝室，如果有显示当前寝室座位
                if(old_dormcode =='' || old_dormcode==null){
                    map = ['cccc','____','cccc'];
                    unavailable = ['1_1','1_2','1_3','1_4','3_1','3_2','3_3','3_4'];
                    select = [];
                    initSeat(map, unavailable,8);
                    $('.front').html('寝室结构图');
                }else{
                    p_dormcode = old_dormcode;
                    getCurrentDormInfo(old_dormcode);
                    getCurrentDormTotal(old_dormcode);
                    $("#dis_dorm").combobox('setText',row['dormvalue']);
                    $("#dis_dorm").combobox('setValue',row['dormcode']);
                    initSeatMap(dormSpec);
                    initSeat(map, unavailable,dormSpec);
                    $('.front').html(row['dormvalue']+'寝室结构图');
                }
                $('#disDorm-dialog').dialog({
                    closed: false,
                    modal: true,
                    title: "分配寝室",
                    top:30,
                    buttons: [{
                        text: '确定',
                        iconCls: 'icon-ok',
                        handler: doDisDorm
                    }, {
                        text: '取消',
                        iconCls: 'icon-cancel',
                        handler: function () {
                            $('#disDorm-dialog').dialog('close');
                        }
                    }]
                });
            }
        }
    }
    function getCurrentDormTotal(dormId){
        $.ajax({
            url: getCurrentDormTotalUrl,
            datatype: 'json',
            type: 'post',
            data:'dormId=' + dormId,
            success: function(result) {
                dormSpec = result[0].spec;
            },
            async: false
        });
    }
    function getCurrentDormInfo(dormId){
        $.ajax({
            url:getCurrentDormInfoUrl,
            data:'dormcode='+dormId,
            datatype:'json',
            type:'post',
            async: false,
            success:function(result){
                if(result.length>0){
                   for(i=0;i<result.length;i++){
                        if(result[i].dormcode!=old_dormcode){
                            unavailable.push(result[i].dormposition);
                        }else{
                            if(result[i].dormposition != dormposition){
                                unavailable.push(result[i].dormposition);
                            }else{
                                select.push(result[i].dormposition);
                            }
                        }
                        studentname[result[i].dormposition] = result[i].studentname;
                   }
                }
            }
        });
    }
    function doDisDorm(){
        if(last_seat =='' && un_select == ''){
            $.messager.show({
                title: '系统提示',
                msg: '请先选择床位'
            });
            return;
        }else{
            if(last_seat!=''){
                // p_dormcode = $("#dis_dorm").combobox('getValue');
                p_dormvalue = $("#dis_dorm").combobox('getText');
                P_dormposition = last_seat;
            }else{
                p_dormcode = -1;
                p_dormvalue = '';
                P_dormposition = '';
            }
            $.ajax({
                url:doDisDormUrl,
                type:'post',
                datatype:'json',
                data:'p_id='+sid+'&p_dormcode='+p_dormcode+'&p_dormvalue='+p_dormvalue+'&P_dormposition='+P_dormposition,
                async:false,
                success:function(result){
                    $.messager.show({
                        title: '系统提示',
                        msg: result.msg
                    });
                    if(result.errorCode == 0){
                        $('#disDorm-dialog').dialog('close');
                        $('#student-datagrid').datagrid('reload');
                    }
                }
            });
        }
    }
    function dormChange(dormId){
        p_dormcode = dormId;
        unavailable = [];
        select = [];
        studentname = new Array();
        last_seat='';
        if(dormId == -1){
            map = ['cccc','____','cccc'];
            unavailable = ['1_1','1_2','1_3','1_4','3_1','3_2','3_3','3_4'];
            initSeat(map, unavailable,8);
            $('.front').html('寝室结构图');
        }else{
            getCurrentDormInfo(dormId);
            getCurrentDormTotal(dormId);
            dormcode = dormId;
            initSeatMap(dormSpec);
            initSeat(map,unavailable,dormSpec);
            $('.front').html($("#dis_dorm").combobox('getText')+'寝室结构图');
        }
    }
    function initSeatMap(count){
        map = [];
        for (i = 1; i <= 3; i++) {
            s = '';
            for (j = 1; j <= count / 2; j++) {
                if (i != 2) {
                    s = s + 'c';
                }
                else {
                    s = s + '_';
                }
            }
            map.push(s);
        }
    }
    function initSeat(map,unavailable,count) {
        sc = $('#seat_area').seatCharts({
        map: map,
        naming: {
            top: false,
            left:false,
            getLabel: function (character, row, column) {
                if(old_dormcode == p_dormcode){
                    if(dormposition == (row + '_' + column)){
                        return '自己';
                    }else{
                        return '空闲';
                    }
                }
                else{
                    return '空闲';
                }
            }
        },
        legend: {
        },
        click: function () {
            if (this.status() == 'select'){
                if(last_seat!=''){
                    sc.get(last_seat).status('available');
                    last_seat='';
                }
                un_select = this.settings.id;
                $('div[class="seatCharts-seat seatCharts-cell select"]').html('调位');
                return 'unselect';
            }else if(this.status() == 'unselect'){
                $('div[class="seatCharts-seat seatCharts-cell unselect"]').html('自己');
                un_select = '';
                return 'select';
            }else if (this.status() == 'available') {
                if (last_seat.length > 0) {
                    sc.get(last_seat).status('available');
                }
                last_seat = this.settings.id;
                un_select ='';
                $('div[class="seatCharts-seat seatCharts-cell unselect"]').html('自己');
                sc.get(select).status('select');
                return 'selected';
            } else if (this.status() == 'selected') {
                last_seat = '';
                un_select = '';
                return 'available';
            } else if (this.status() == 'unavailable') {
                return 'unavailable';
            } else {
                return this.style();
            }
        }
        });
        sc.get(unavailable).status('unavailable');
        sc.get(select).status('select');
        $('div[class="seatCharts-seat seatCharts-cell unavailable"]').html('有人');
        switch(parseInt(count)){
            case 4: $('div[class*="seatCharts-cell"]').css('margin','0px 62px auto');break;
            case 6: $('div[class*="seatCharts-cell"]').css('margin','0px 35px auto');break;
            case 8: $('div[class*="seatCharts-cell"]').css('margin','0px 21px auto');break;
            case 10: $('div[class*="seatCharts-cell"]').css('margin','0px 13px auto');break;
        }
        $('div[class="seatCharts-seat seatCharts-cell unavailable"]').bind("mouseover", function () {
            id = $(this).attr("id");
            if (!(studentname === undefined)) {
                $(this).prop("title", studentname[id]);
            }
        });
    }
    //导出数据
    function exportData(){
        //获取datagrid中数据总数
        total = $('#student-datagrid').datagrid("getData").total;
        if(total > 0){
            departmentCode = $("#q_department").combobox('getValue');
            majorCode = $("#q_major").combobox('getValue');
            dormCode = $("#q_dorm").combobox('getValue');
            studentName = $.trim($('input[textboxname=q_studentName]').textbox('getValue'));
            $.ajax({
                url:doExportUrl,
                data:'departmentCode='+departmentCode+'&majorCode='+majorCode+'&dormCode='+dormCode+'&studentName='+studentName,
                datatype:'json',
                type:'post',
                beforeSend: function () {
                    load('正在处理，请稍待...');
                },
                complete: function () {
                    disLoad();
                },
                success:function(result){
                    location.href="../"+result.url;
                }
            });
        }else{
            $.messager.show({
                title: '系统提示',
                msg: '没有可以导出的数据'
            });
        }
    }
</script>